﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using SFML.Graphics;
using System.Windows.Forms;
using SFML.Window;
using Color = SFML.Graphics.Color;
using Image = SFML.Graphics.Image;

namespace Strom.MapEditor.Graphic
{
    public static class Functions
    {
        // Affichage d'un bout de tiles
        public static void DrawTiles(short mapNum, byte X, byte Y, byte Layer)
        {
            byte tX = 0;
            byte tY = 0;

            // Vérification de la couche
            switch (Layer)
            {
                case 0 :
                    Var.Tiles = new Sprite(GetTiles(Var.GameWorld.Maps[mapNum].Cells[X,Y].Sol.TilesNum));
                    tX = Var.GameWorld.Maps[mapNum].Cells[X, Y].Sol.TilesX;
                    tY = Var.GameWorld.Maps[mapNum].Cells[X, Y].Sol.TilesY;
                    break;
                case 1 :
                    Var.Tiles = new Sprite(GetTiles(Var.GameWorld.Maps[mapNum].Cells[X,Y].Inf[0].TilesNum));
                    tX = Var.GameWorld.Maps[mapNum].Cells[X, Y].Inf[0].TilesX;
                    tY = Var.GameWorld.Maps[mapNum].Cells[X, Y].Inf[0].TilesY;
                    break;
                case 2 :
                    Var.Tiles = new Sprite(GetTiles(Var.GameWorld.Maps[mapNum].Cells[X,Y].Inf[1].TilesNum));
                    tX = Var.GameWorld.Maps[mapNum].Cells[X, Y].Inf[1].TilesX;
                    tY = Var.GameWorld.Maps[mapNum].Cells[X, Y].Inf[1].TilesY;
                    break;
                case 3 :
                    Var.Tiles = new Sprite(GetTiles(Var.GameWorld.Maps[mapNum].Cells[X,Y].Inf[2].TilesNum));
                    tX = Var.GameWorld.Maps[mapNum].Cells[X, Y].Inf[2].TilesX;
                    tY = Var.GameWorld.Maps[mapNum].Cells[X, Y].Inf[2].TilesY;
                    break;
                case 4 :
                    Var.Tiles = new Sprite(GetTiles(Var.GameWorld.Maps[mapNum].Cells[X,Y].Sup[0].TilesNum));
                    tX = Var.GameWorld.Maps[mapNum].Cells[X, Y].Sup[0].TilesX;
                    tY = Var.GameWorld.Maps[mapNum].Cells[X, Y].Sup[0].TilesY;
                    break;
                case 5 :
                    Var.Tiles = new Sprite(GetTiles(Var.GameWorld.Maps[mapNum].Cells[X,Y].Sup[1].TilesNum));
                    tX = Var.GameWorld.Maps[mapNum].Cells[X, Y].Sup[1].TilesX;
                    tY = Var.GameWorld.Maps[mapNum].Cells[X, Y].Sup[1].TilesY;
                    break;
                case 6 :
                    Var.Tiles = new Sprite(GetTiles(Var.GameWorld.Maps[mapNum].Cells[X,Y].Sup[2].TilesNum));
                    tX = Var.GameWorld.Maps[mapNum].Cells[X, Y].Sup[2].TilesX;
                    tY = Var.GameWorld.Maps[mapNum].Cells[X, Y].Sup[2].TilesY;
                    break;
                }

            // Affichage
            Var.Tiles.TextureRect = new IntRect(tX * 32,tY * 32,32,32);
            Var.Tiles.Position = new Vector2f(X*32,Y*32);
            if (Var.Tiles.Texture != null)
            {
                Var.RenderWin.Draw(Var.Tiles);
            }
            Var.Tiles.Dispose();
        }

        // Récupération du tiles à afficher
        public static Texture GetTiles(string tilesName)
        {
            for (byte i = 0; i < Var.TilesList.Length; i++)
            {
                if (tilesName == Var.TilesList[i])
                {
                    return Var.TexTiles[i];
                }
            }

            return Var.DefautTexture;
        }

        // Changement de la planche de tiles
        public static void ChangeTileset(byte index)
        {
            MapEditorManager.GUI.Editor.lstTiles.SelectedIndex = index;
            MapEditorManager.GUI.Editor.picTiles.Image = new Bitmap("Graphique\\Tiles\\" + MapEditorManager.GUI.Editor.lstTiles.Items[index]);

        }

        // Place le carré de tiles sur le picscreen
        public static void PlaceTiles(short mapnum, byte layer)
        {
            if (layer == 0)
            {
                try
                {
                    if (Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sol.TilesNum ==
                        MapEditorManager.GUI.Editor.lstTiles.Text &&
                        Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sol.TilesX == Var.TilesX &&
                        Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sol.TilesY == Var.TilesY)
                        return;
                }
                catch (IndexOutOfRangeException)
                {
                    return;
                }
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sol.TilesNum = MapEditorManager.GUI.Editor.lstTiles.Text;
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sol.TilesX = (byte)Var.TilesX;
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sol.TilesY = (byte)Var.TilesY;
            }
            else if (layer == 1)
            {
                try
                {
                    if (Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[0].TilesNum ==
                        MapEditorManager.GUI.Editor.lstTiles.Text &&
                        Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[0].TilesX == Var.TilesX &&
                        Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[0].TilesY == Var.TilesY)
                        return;
                }
                catch (IndexOutOfRangeException)
                {
                    return;
                }
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[0].TilesNum = MapEditorManager.GUI.Editor.lstTiles.Text;
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[0].TilesX = (byte)Var.TilesX;
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[0].TilesY = (byte)Var.TilesY;  
            }
            else if (layer == 2)
            {
                try
                {
                    if (Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[1].TilesNum ==
                        MapEditorManager.GUI.Editor.lstTiles.Text &&
                        Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[1].TilesX == Var.TilesX &&
                        Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[1].TilesY == Var.TilesY)
                        return;
                }
                catch (IndexOutOfRangeException)
                {
                    return;
                }
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[1].TilesNum = MapEditorManager.GUI.Editor.lstTiles.Text;
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[1].TilesX = (byte)Var.TilesX;
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[1].TilesY = (byte)Var.TilesY;  
            }
            else if (layer == 3)
            {
                try
                {
                    if (Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[2].TilesNum ==
                        MapEditorManager.GUI.Editor.lstTiles.Text &&
                        Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[2].TilesX == Var.TilesX &&
                        Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[2].TilesY == Var.TilesY)
                        return;
                }
                catch (IndexOutOfRangeException)
                {
                    return;
                }
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[2].TilesNum = MapEditorManager.GUI.Editor.lstTiles.Text;
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[2].TilesX = (byte)Var.TilesX;
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Inf[2].TilesY = (byte)Var.TilesY;  
            }
            else if (layer == 4)
            {
                try
                {
                    if (Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[0].TilesNum ==
                        MapEditorManager.GUI.Editor.lstTiles.Text &&
                        Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[0].TilesX == Var.TilesX &&
                        Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[0].TilesY == Var.TilesY)
                        return;
                }
                catch (IndexOutOfRangeException)
                {
                    return;
                }
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[0].TilesNum = MapEditorManager.GUI.Editor.lstTiles.Text;
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[0].TilesX = (byte)Var.TilesX;
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[0].TilesY = (byte)Var.TilesY;  
            }
            else if (layer == 5)
            {
             try
                {
                      if (Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[1].TilesNum ==
                        MapEditorManager.GUI.Editor.lstTiles.Text &&
                        Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[1].TilesX == Var.TilesX &&
                        Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[1].TilesY == Var.TilesY)
                        return;
                }
                catch (IndexOutOfRangeException)
                {
                    return;
                }
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[1].TilesNum = MapEditorManager.GUI.Editor.lstTiles.Text;
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[1].TilesX = (byte)Var.TilesX;
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[1].TilesY = (byte)Var.TilesY;  
            }
            else if (layer == 6)
            {
                try
                {
                    if (Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[2].TilesNum ==
                      MapEditorManager.GUI.Editor.lstTiles.Text &&
                      Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[2].TilesX == Var.TilesX &&
                      Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[2].TilesY == Var.TilesY)
                        return;
                }
                catch (IndexOutOfRangeException)
                {
                    return;
                }
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[2].TilesNum = MapEditorManager.GUI.Editor.lstTiles.Text;
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[2].TilesX = (byte)Var.TilesX;
                Var.GameWorld.Maps[mapnum].Cells[Var.CursorX, Var.CursorY].Sup[2].TilesY = (byte)Var.TilesY;  
            }
        }
    }
}
